package cn.wizzer.app.web.modules.controllers.platform.sys;

import cn.wizzer.app.sys.modules.models.SysParam;
import cn.wizzer.app.sys.modules.services.SysParamService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/sys/param")
public class SysParamController {
    private static final Log log = Logs.get();
    @Inject
    @Reference
    private SysParamService sysParamService;
    @Inject
    @Reference
    private UserService userService;

    @At("")
    @Ok("beetl:/platform/sys/param/index.html")
    @RequiresPermissions("sys.manager.param")
    public void index() {

    }

    @At
    @Ok("json")
    @RequiresPermissions("sys.manager.param.edit")
    @SLog(tag = "修改参数", msg = "${param}")
    public Object editDo(@Param("..") SysParam param) {
        sysParamService.edit(param);
        if(Strings.isNotBlank(param.getDefaultHeadPicture())){
            userService.update(Chain.make("headPicture", param.getDefaultHeadPicture()),
                    Cnd.where("headPicture", "is", null)
                    .or("headPicture", "=", ""));
        }
        return Result.success();
    }

    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.param")
    public Object data() {
        return Result.success().addData(sysParamService.fetch());
    }

}
